import asyncio


class Reader(object):
    """自定义异步迭代器"""

    def __init__(self):
        self.count = 0

    async def read_line(self):
        #await asyncio.sleep(1)
        self.count += 1
        if self.count == 100:
            return None
        return self.count

    def __aiter__(self):
        return self

    async def __anext__(self):
        line = await self.read_line()
        if line is None:
            raise StopAsyncIteration
        return line


async def func():
    reader = Reader()
    """异步迭代器的迭代写法。注意：async for只能写在协程函数中，不能在外部直接写"""
    async for line in reader:
        print(line)

asyncio.run(func())
